package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    private static final Integer LAUNCH = 1;    // 启售
    private static final Integer DISABLE = 0;   // 停售


    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        /**
         * 营业额：当日已完成订单的总金额
         * 有效订单：当日已完成订单的数量
         * 订单完成率：有效订单数 / 总订单数
         * 平均客单价：营业额 / 有效订单数
         * 新增用户：当日新增用户的数量
         */
        Map map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);

        // 查询新增用户数
        Integer newUsers = userMapper.countByMap(map);

        // 查询总订单数
        Integer totalOrderCount = orderMapper.countByMap(map);
        // 查询有效订单数
        map.put("status", Orders.COMPLETED);
        Integer validOrderCount = orderMapper.countByMap(map);
        // 计算订单完成率
        Double orderCompletionRate = 0.0;
        if (totalOrderCount > 0) {
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
        }
        // 查询营业额
        Double turnover = 0.0;
        Double initTurnover = orderMapper.sumByMap(map);
        if(!ObjectUtils.isEmpty(initTurnover)) {
            turnover = initTurnover;
        }
        // 计算平均客单价
        Double unitPrice = 0.0;
        if (validOrderCount > 0) {
            unitPrice = turnover / validOrderCount;
        }

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }

    @Override
    public OrderOverViewVO getOverview() {
        /**
         * 订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
         */
        Map map = new HashMap();
        map.put("begin", LocalDateTime.now().with(LocalTime.MIN));

        // 总订单数
        Integer totalOrders = orderMapper.countByMap(map);

        // 待接单
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.countByMap(map);

        // 待派送
        map.put("status", Orders.CONFIRMED);
        Integer confirmed = orderMapper.countByMap(map);

        // 已完成
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.countByMap(map);

        // 已取消
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countByMap(map);

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(confirmed)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(totalOrders)
                .build();
    }

    @Override
    public DishOverViewVO getDishesOverview() {
        LocalDateTime end = LocalDateTime.now();
        Map map = new HashMap();
        map.put("end", end);
        // 菜品启售数量
        map.put("status", LAUNCH);
        Integer launchCount = dishMapper.countByMap(map);

        // 菜品停售数量
        map.put("status", DISABLE);
        Integer disabledCount = dishMapper.countByMap(map);
        return DishOverViewVO.builder()
                .sold(launchCount)
                .discontinued(disabledCount)
                .build();
    }

    @Override
    public SetmealOverViewVO getSetmealsOverview() {
        Map map = new HashMap();
        map.put("end", LocalDateTime.now());
        // 菜品启售数量
        map.put("status", LAUNCH);
        Integer launchCount = setmealMapper.countByMap(map);

        // 菜品停售数量
        map.put("status", DISABLE);
        Integer disabledCount = setmealMapper.countByMap(map);
        return SetmealOverViewVO.builder()
                .sold(launchCount)
                .discontinued(disabledCount)
                .build();
    }

}
